﻿using System.Collections.Generic;
using Caliburn.Micro;
using Zero.Common;

namespace Zero.Frontend
{
    /// <summary>
    /// Helper extension methods for cleaner code.
    /// </summary>
    public static class ExtensionMethods
    {
        //public static IResult ShowAsDialog(this IScreen screen)
        //{
        //    return new ShowDialogResult(screen);
        //}

        /// <summary>
        /// Executes one coroutine as part of another coroutine.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static IResult Execute(this IEnumerable<IResult> e)
        {
            return Coroutine.CreateParentEnumerator(e.GetEnumerator());
        }
        
        /// <summary>
        /// Creates <see cref="BackendResult{TResult}"/> from transport object, that implements <see cref="IRequest{TResponse}"/>.
        /// </summary>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="request"></param>
        /// <param name="catchException"><c>true</c> if this result should catch exceptions. <c>false</c> if exceptions should be left to coroutine. Default is <c>true</c>.</param>
        /// <returns></returns>
        public static BackendResult<TResponse> AsRequest<TResponse>(this IRequest<TResponse> request, bool catchException = true)
        {
            return new BackendResult<TResponse>(request, catchException);
        }

        //public static InitializationResult InitializeAsync(this IAsyncInicializable inicializable)
        //{
        //    return new InitializationResult(inicializable.InicializeAsync());
        //}
    }
}
